package cn.tom.controller.adm;

import cn.tom.dao.LargeFileDao;
import cn.tom.dao.UserDao;
import cn.tom.entity.Largefile;
import cn.tom.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/admtea")
public class AdmTeaController {
    @Autowired(required = false)
    UserDao userDao;
    @Autowired
    LargeFileDao largeFileDao;
    @RequestMapping("/findAll")
    public String findall(Model model, HttpServletResponse response) throws IOException {
        List<User> users =userDao.findOneByRole("tea");
        model.addAttribute("user",users);
        return "adm/lagintea";
    }
    @GetMapping("/go2add")
    public String go2add(Model model){
        List<User> users =userDao.findOneByRole("tea");
        model.addAttribute("user",users);
        model.addAttribute("action","add");
        return "adm/lagintea";
    }
    @PostMapping("/add")//添加
    public String add(User user, @RequestParam(value = "pic",required = true)MultipartFile file, Model model, HttpSession session) {
        System.out.println("user=" + user);
//        没有图片的情况，保存图片和学生
        if (file == null || file.isEmpty() == true) {
            System.out.println("没有图片");
            userDao.add(user);
            return "adm/lagintea";
        }
        String uuid = UUID.randomUUID().toString();
        try {
            Largefile largefile = new Largefile();
            System.out.println("uuid=" + uuid);
            largefile.setId(uuid);
            largefile.setFilename(file.getOriginalFilename());
            System.out.println(largefile.getFilename());
            byte[] buf = file.getBytes();
            System.out.println("buf.length=" + buf.length);
            largefile.setContent(buf);
            largeFileDao.add(largefile);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "err";
        }
        Object who = session.getAttribute("who");
        System.out.println("who="+who);
        User user1 = (User) who;
        List<User> users =userDao.findOneByRole(user1.getRole());
        model.addAttribute("user",users);
        user.setPic(uuid);
        user.setRole("stu");
        userDao.add(user);
        return "adm/lagintea";
    }

    @GetMapping ("/go2update")
    public String update(Model model,int uid){
        User user1=userDao.fiduid(uid);
        model.addAttribute("userp",user1);
        List<User> users =userDao.findOneByRole("tea");
        model.addAttribute("user",users);
        model.addAttribute("action","update");
        return "adm/lagintea";
    }
    @PostMapping("/update")
    public String update(User user , @RequestParam(value = "pic",required = true)MultipartFile file, Model model)throws IOException{
        System.out.println("更新的数据"+user);
        User user2 =userDao.fiduid(user.getUid());
        System.out.println("查询图片="+user2);
        largeFileDao.delete((String) user2.getPic());
        try {
            Largefile largefile = new Largefile();
            Object uuid= user2.getPic();
            System.out.println(uuid);
            largefile.setId((String) uuid);
            largefile.setFilename(file.getOriginalFilename());

            System.out.println("largefile.getFilename()=="+largefile.getFilename());
            byte[] buf = file.getBytes();
            System.out.println("buf.length=" + buf.length);
            largefile.setContent(buf);
            System.out.println("11="+largefile.getContent());
            largeFileDao.add(largefile);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "err";
        }
        System.out.println(userDao.update(user));

        return "adm/lagintea";
    }
    @GetMapping("/del")
    public String delect(int uid, Model model){
        int dele=userDao.del(uid);
        System.out.println(dele);
        List<User> users =userDao.findOneByRole("tea");
        model.addAttribute("user",users);
        return "adm/lagintea";
    }
    @GetMapping("/showimg/{pic}")
    public void  showimg(@PathVariable("pic")String id,
                         HttpServletResponse response) throws IOException {
        Largefile one =largeFileDao.findOne(id);
        Object conent=one.getContent();

        OutputStream os=response.getOutputStream();
        response.setContentType("image/jpg");
//
        byte[] buf= (byte[])conent;
        os.write(buf);
        os.flush();

    }
}
